package com.bubblegumproject.ogo.action;

import com.bubblegumproject.ogo.component.Container;
import com.bubblegumproject.ogo.component.injection.Inject;
import com.bubblegumproject.ogo.component.PostConstruct;

/**
 * A complement implementation of the {@link Flow} contract. This class provides access to a {@link Container}
 * that a Flow may use to create components it needs to do its work. This class also provides a {@link #start()}
 * method that will create and initialize a {@link SimpleContext} for the Flow.
 *
 * @author Azubuko Obele (buko.obele@gmail.com)
 */
public class SimpleFlow extends AbstractFlow {

    /**
     * Set the Container for the Flow to use to accomplish its work. Most Flow objects require a Container to create
     * components that can then be used to execute application logic. This may not be the Container that created the
     * Flow.
     * <p/>
     * This property is usually autoinjected when a Flow component is created by a Container.
     * <p/>
     * TODO: We may not need this @Inject here. As a rule we would expect anybody who creates a Flow to set the Context
     * and the Container
     *
     * @param container Container that a AbstractFlow may use to do its work
     */
    @Inject
    public void setContainer(Container container) {
        this.container = container;
    }


    @Inject
    public void setParent(AbstractFlow parent) {
        this.parent = parent;
    }

    /**
     * Initialize the Context for this Flow. This is done by invoking the {@link #createContext()} method.
     */
    @PostConstruct
    public void start() {
        String prefix = getClass().getSimpleName() + ".";
        context.set(prefix + "container", container);
        context.set(prefix + "flow", this);
    }

    protected Context createContext() {
        // XXX: This is just wrong. A SimpleFlow must inherit the Action Context used to create it, not the Container!
        // Get rid of this method.
        Context context = new SimpleContext(parent.getContext());
        ((SimpleContext) context).setName(getClass().getName());
        String prefix = getClass().getSimpleName() + ".";
        context.set(prefix + "container", container);
        context.set(prefix + "flow", this);
        return context;
    }
}
